package co.gov.medellin.ssti.sub.common.util;

import java.sql.Connection;
import java.sql.SQLException;

import co.gov.medellin.ssti.sub.common.ConnectionFactory;
import co.gov.medellin.ssti.sub.common.excepcion.SUBException;

/**
 * Clase utility
 * 
 * @author Consultsoft4
 * 
 */
public class ControlExceptions {

	/**
	 * Conexion a la base de datos.
	 */
	private static Connection conn = null;

	public ControlExceptions(ConnectionFactory cf) {

	}

	/**
	 * Metodo que realiza un insert de la excepcion en TCS_BEN_LOG.
	 * 
	 * @param e
	 *            Excepcion a ser persistida.
	 * @param cf
	 *            ConnectionFactory
	 */
	public static void insertException(Exception e, ConnectionFactory cf) {
		try {
			conn = cf.getConnection();
			StackTraceElement[] ste = e.getStackTrace();
			String strMensaje = "Trace:";
			for (int i = 0; i < ste.length; i++)
				strMensaje += "\r" + ste[i].toString();

			String query = " INSERT INTO TCS_BEN_LOG "
					+ " (CODIGO, FECHA_ORIGEN, "
					+ " MENSAJE_ERROR, PILA_ERROR) "
					+ " VALUES(SCS_BEN_LOG.NextVal, to_date(sysdate,'dd/mm/rrrr hh:mi:ss'),?,?)";

			String strArray[] = { e.getMessage(), strMensaje };
			int iRegInsert = cf.execSql(query, strArray, conn);

		} catch (SQLException ex) {
			SUBException.logDebugError("", ex);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception exc) {
					SUBException.logDebugError("", exc);
				}
			}
		}
	}
}
